package com.azkj.album.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.azkj.album.R;
import com.azkj.album.dto.NewGalleryBean;
import com.azkj.album.network.utils.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;

public class ClassPhotoSelectAdapter extends BaseQuickAdapter<NewGalleryBean, BaseViewHolder> {
    private Context mContext;
    private ItemOnClickInterface itemOnClickInterface;
    private ArrayList<String> selectedList = new ArrayList<>();
    private int type;

    public void setType(int type) {
        this.type = type;
    }

    public ClassPhotoSelectAdapter(Context context, int layoutResId) {
        super(layoutResId);
        mContext = context;
    }

    //回调接口
    public interface ItemOnClickInterface {
        void onItemClick(String imageUrl, boolean isSelected);
    }

    //定义回调方法
    public void setItemOnClickInterface(ItemOnClickInterface itemOnClickInterface) {
        this.itemOnClickInterface = itemOnClickInterface;
    }

    @Override
    protected void convert(BaseViewHolder helper, NewGalleryBean item) {
        ImageView image = helper.getView(R.id.iv_image);
        if (type == 2){
            Glide.with(mContext).load(item.getFullUrl())
                    .into(image);
        }else {
            Glide.with(mContext).load(item.getImgPath())
                    .skipMemoryCache(true) // 不使用内存缓存
                    .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                    .into(image);
        }
        CheckBox image_select = helper.getView(R.id.iv_image_select);
        if (item.getSelect()) {
            image_select.setButtonDrawable(R.mipmap.ic_new_photo_yes_select);
        } else {
            image_select.setButtonDrawable(R.mipmap.ic_new_photo_no_select);
        }
//        image_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//                if (isChecked) {
//                    item.setSelect(true);
//                } else {
//                    item.setSelect(false);
//                }
//                itemOnClickInterface.onItemClick(image_select);
//                notifyItemChanged(helper.getAdapterPosition());
//            }
//        });
        image_select.setOnClickListener(v -> helper.itemView.performClick());
        helper.itemView.setOnClickListener(v -> {
            if (!item.getSelect()) {
                if (selectedList.size() >= 6){
                    ToastUtils.showCenterToast("最多添加6张");
                }else {
                    item.setSelect(true);
                    selectedList.add(item.getImgPath());
                }
            } else {
                item.setSelect(false);
                selectedList.remove(item.getImgPath());
            }
            itemOnClickInterface.onItemClick(type == 2 ? item.getFullUrl() : item.getImgPath(), item.getSelect());
            notifyItemChanged(helper.getAdapterPosition());
        });
    }
}
